﻿using Ext.Net;
using NOAH.CMS.Cache;
using NOAH.CMS.Repository;

using System;
using System.Web.Profile;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.HtmlControls;

public partial class admin_Modules_NewsEvent_NewsCategory_AddOrEdit : System.Web.UI.Page
{

    private string id = string.Empty;
    private string mode = string.Empty;

    protected void AddOrEditCategory(object sender, DirectEventArgs e)
    {
        NewsCategoryRepository repository = new NewsCategoryRepository();
        try
        {
            NewsCategory newProductCategory = null;
            if (this.mode == "add")
            {
                newProductCategory = new NewsCategory
                {
                    CreatedBy = BasePage.GetCurrentUserName(),
                    CreatedOn = new DateTime?(DateTime.Now)

                };
            }
            else
            {
                newProductCategory = repository.Get(int.Parse(this.id));
            }


            newProductCategory.EditedBy = BasePage.GetCurrentUserName();
            newProductCategory.EditedOn = new DateTime?(DateTime.Now);
            newProductCategory.Name = this.txtTitle.Text;
            newProductCategory.Link = this.txtUrl.Text;
            newProductCategory.IsActive = new bool?(this.chkActive.Checked);
            newProductCategory.IsOnHomePage = new bool?(this.chkOnHomePage.Checked);
            newProductCategory.IsBottom = new bool?(this.chkOnBottom.Checked);
            newProductCategory.IsLeft = new bool?(this.chkOnLeft.Checked);
            newProductCategory.IsRight = new bool?(this.chkOnRight.Checked);
            newProductCategory.IsTop = new bool?(this.chkOnTop.Checked);

            newProductCategory.Target = "_self";
            if (chkNewWindow.Checked)
            {
                newProductCategory.Target = "_blank";
            }

            this.chkNewWindow.Checked = false;
            if (newProductCategory.Target == "_bank")
            {
                this.chkNewWindow.Checked = true;
            }

            if (this.txtOrder.Value != null)
            {
                newProductCategory.Order = new short?(short.Parse(this.txtOrder.Value.ToString()));
            }

            if (this.mode == "add")
            {
                newProductCategory.ParentId = int.Parse(this.id);
                repository.Add(newProductCategory);
            }
            repository.SubmitChanges();
            new NewsCategoryCacheLayer().InvalidateCache();
            BasePage.RegisterCallBackFromChild(this.SM, string.Empty);
            if (this.mode == "add")
            {
                this.ResetControl();
            }
            else
            {
                BasePage.RegisterCloseWindow(this.SM);
            }


            ExtNet.Msg.Hide();
            Util.ShowMessage(Constants.UPDATE_SUCCESS);
        }
        catch (Exception ex)
        {
            Util.ShowMessage2(ex.StackTrace);

            //X.Msg.Hide();
            //Util.ShowMessage2(Constants.UPDATE_FAILURE);
        }
        finally
        {
            if (repository != null)
            {
                repository.Dispose();
            }
        }
    }

    protected void LoadAddOrEditCategoryForm()
    {
        if (this.mode == "edit")
        {
            using (NewsCategoryRepository repository = new NewsCategoryRepository())
            {
                NewsCategory detailCategory = repository.Get(int.Parse(this.id));
                this.txtTitle.Text = detailCategory.Name;
                this.txtUrl.Text = detailCategory.Link;
                this.chkActive.Checked = detailCategory.IsActive.HasValue ? detailCategory.IsActive.Value : false;
                this.chkOnHomePage.Checked = detailCategory.IsOnHomePage.HasValue ? detailCategory.IsOnHomePage.Value : false;
                bool? temp = detailCategory.IsBottom;
                this.chkOnBottom.Checked = temp.HasValue ? temp.GetValueOrDefault() : false;
                temp = detailCategory.IsLeft;
                this.chkOnLeft.Checked = temp.HasValue ? temp.GetValueOrDefault() : false;
                temp = detailCategory.IsRight;
                this.chkOnRight.Checked = temp.HasValue ? temp.GetValueOrDefault() : false;
                temp = detailCategory.IsTop;
                this.chkOnTop.Checked = temp.HasValue ? temp.GetValueOrDefault() : false;
                short? temp2 = detailCategory.Order;
                this.txtOrder.Value = temp2.HasValue ? ((object)temp2.GetValueOrDefault()) : ((object)0);
                if (string.IsNullOrEmpty(detailCategory.Target))
                {
                    this.chkNewWindow.Checked = false;
                }
                else if (detailCategory.Target == "_self")
                {
                    this.chkNewWindow.Checked = false;
                }
                else
                {
                    this.chkNewWindow.Checked = false;
                }
            }
        }
        this.wdAddOrEdiCategory.Show();
        ExtNet.Msg.Hide();
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        mode = base.Request.QueryString["mode"];

        id = base.Request.QueryString["id"] ?? "0";

        if (!ExtNet.IsAjaxRequest)
        {
            this.LoadAddOrEditCategoryForm();
        }
    }

    private void ResetControl()
    {
        this.txtTitle.Reset();
        this.txtUrl.Reset();
        this.chkNewWindow.Reset();
    }

}